package com.foton.boot;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;


/**
 * @Author RenPu
 * @Date 2023/3/28 14:59
 * @Version 1.0
 * @Description:
 *     根据servlet3.0，实现org.springframework.web.SpringServletContainerInitializer的子类
 *     tomcat启动的时候就会进行加载自定义的实现，从而实现了替代web.xml文件的形式
 *     rg.springframework.web.SpringServletContainerInitializer，存在spring-web的jar中META-INF/services/javax.servlet.ServletContainerInitializer文件内
 **/
public class MyWebApplicationInitializer implements WebApplicationInitializer {


    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        // Load Spring web application configuration
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(CommonConfig.class);
        context.refresh();

        // Create and register the DispatcherServlet
        DispatcherServlet servlet = new DispatcherServlet(context);

        //将servlet命名为app放入容器
        ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
        registration.setLoadOnStartup(1);       //启动顺序
        registration.addMapping("/*");  //拦截所有方法

    }
}
